package com.uivista.honeycomb;

import com.wowza.wms.amf.AMFDataList;
import com.wowza.wms.amf.AMFDataObj;
import com.wowza.wms.application.IApplicationInstance;
import com.wowza.wms.client.IClient;
import com.wowza.wms.module.ModuleBase;
import com.wowza.wms.request.RequestFunction;
import com.wowza.wms.stream.publish.IStreamActionNotify;
import com.wowza.wms.stream.publish.Playlist;
import com.wowza.wms.stream.publish.PlaylistItem;
import com.wowza.wms.stream.publish.Stream;

public class VodStream extends ModuleBase {

	public void openPlaylistOnStream(IClient client, RequestFunction function, AMFDataList params)
	  {
	    String streamName = getParamString(params, 3);
	    String playlistName = getParamString(params, 4);
	    Stream stream = (Stream)client.getAppInstance().getProperties().getProperty(streamName);
	    Playlist playlist = (Playlist)client.getAppInstance().getProperties().getProperty(playlistName);

	    IStreamActionNotify actionNotify = new StreamListener(client.getAppInstance());
	    stream.addListener(actionNotify);

	    playlist.open(stream);
	  }

	  public void addItemToPlaylist(IClient client, RequestFunction function, AMFDataList params)
	  {
	    AMFDataObj obj = getParamObj(params, 3);
	    Playlist playlist = (Playlist)client.getAppInstance().getProperties().getProperty(obj.getString("playListName"));
	    playlist.addItem(obj.getString("itemName"), obj.getInt("itemStart"), obj.getInt("itemDuration"));
	  }

	  public void playNextPlaylistItem(IClient client, RequestFunction function, AMFDataList params)
	  {
	    String streamName = getParamString(params, 3);
	    Stream stream = (Stream)client.getAppInstance().getProperties().getProperty(streamName);
	    stream.next();
	  }

	  public void addNewStream(IClient client, RequestFunction function, AMFDataList params)
	  {
	    String streamName = getParamString(params, 3);
	    Stream stream = Stream.createInstance(client.getAppInstance(), streamName);
	    client.getAppInstance().getProperties().put(streamName, stream);

	    client.getProperties().setProperty("streamName", streamName);
	  }

	  public void addNewPlaylist(IClient client, RequestFunction function, AMFDataList params)
	  {
	    String playListName = getParamString(params, 3);
	    Playlist playlist = new Playlist(playListName);
	    playlist.setRepeat(false);
	    client.getAppInstance().getProperties().put(playListName, playlist);
	  }

	  public void stopStream(IClient client, RequestFunction function, AMFDataList params)
	  {
	    String streamName = getParamString(params, 3);
	    stopStreamHandler(client, streamName);
	  }

	  public void stopStreamHandler(IClient client, String streamName)
	  {
	    Stream stream = (Stream)client.getAppInstance().getProperties().remove(streamName);
	    if (stream != null) {
	      stream.close();
	    }
	    client.getProperties().setProperty("streamName", null);
	  }

	  public void onDisconnect(IClient client)
	  {
	    String streamName = client.getProperties().getPropertyStr("streamName");
	    stopStreamHandler(client, streamName);
	  }

	  class StreamListener implements IStreamActionNotify
	  {
	    StreamListener(IApplicationInstance appInstance)
	    {
	    }

	    public void onPlaylistItemStop(Stream stream, PlaylistItem item) {
	      stream.close();
	    }

	    public void onPlaylistItemStart(Stream stream, PlaylistItem item)
	    {
	    }
	  }

}
